<?php
class Blog extends DBSQL
{
   public $pagesize;
   public function __construct()
   {
		parent::__construct();
		$this->pagesize = 10;
   }
   /**
	 * 功能：按条件提取文章列表
	 * 参数：$blogid 博客ID,$categories 分类ID,$keyword 关键字,$date 日期
	 * 参数：$status 提取文章的状态,$page 当前页码
	 * 返回：数组
	 */
	public function GetPostList($blogid, $categories=0, $keyword='', $date='', $status=0, $page=1, $pagesize=0, $cat1FID=0)
	{
		if(!$pagesize)
			$pagesize = $this->pagesize;
		$start = ($page - 1) * $pagesize;

		$sql = "SELECT c.F_CATEGORIES_NAME, c.F_ID as CatId, p.F_ID_CAT1, p.F_POSTS_IS_COMMENTED, p.F_POSTS_TITLE, p.F_POSTS_CONTENTS, p.F_ID, p.F_POSTS_COMMENTS, p.F_POSTS_ISSUE_DATE, p.F_POSTS_VIEWS FROM ee_blog_categories2 c, ee_blog_posts p ";
		$sql .= "WHERE c.F_ID_BLOG_INFO = $blogid AND c.F_ID = p.F_ID_CATEGORIES";
		if($categories > 0)
			$sql .= " AND c.F_ID = $categories";
		if($cat1FID > 0)
			$sql .= " AND p.F_ID_CAT1 = $cat1FID";
		if($keyword)
			$sql .= " AND (p.F_POSTS_TITLE like '%$keyword%' OR p.F_POSTS_CONTENTS like '%$keyword%')";
		if($date)
		{
			list($year, $month, $day) = explode("-", $date);
			$start_date = mktime(0,0,0,$month,$day,$year);
			$end_date = mktime(23,59,59,$month,$day,$year);
			$sql .= " AND (p.F_POSTS_ISSUE_DATE >= $start_date AND p.F_POSTS_ISSUE_DATE <= $end_date)";
		}

		switch($status)
		{
			case 1:
				$sql .= " AND p.F_POSTS_STATUS = 1";
				break;
			case 2:
				$sql .= " AND (p.F_POSTS_STATUS = 1 OR p.F_POSTS_STATUS = 2)";
				break;
			default:
				break;
		}
		$sql .= " ORDER BY p.F_POSTS_ISSUE_DATE DESC LIMIT $start,$pagesize";
		return $this->select($sql);
	}
	/**
	 * 功能：提取文章标题
	 * 参数：$blogid 博客ID, $keyword 关键字
	 * 返回：二维数组
	 */
	public function GetPostTitle($blogid, $keyword = ''){
		$sql = "SELECT F_POSTS_TITLE FROM ee_blog_posts WHERE F_ID_USER_INFO = $blogid";
		if($keyword) $sql .= " AND F_POSTS_TITLE like '%$keyword%'";
		return $this->select($sql);
	}
	/**
	 * 功能：提取文章条数
	 * 参数：$blogid 博客ID,$categories 分类ID,$keyword 关键字,$date 日期
	 * 参数：$status 提取文章的状态,$page 当前页码
	 * 返回：整数
	 */
	public function GetPostCount($blogid, $categories= 0, $keyword='', $date='', $status=0, $cat1FID=0)
	{
		$sql = "SELECT count(p.F_ID) FROM ee_blog_categories2 c,ee_blog_posts p";
		$sql .= " WHERE c.F_ID_BLOG_INFO = $blogid AND c.F_ID = p.F_ID_CATEGORIES";
		if($categories > 0)
			$sql .= " AND c.F_ID = $categories";
		if($cat1FID > 0)
			$sql .= " AND p.F_ID_CAT1 = $cat1FID";
		if($keyword)
			$sql .= " AND (p.F_POSTS_TITLE like '%$keyword%' OR p.F_POSTS_CONTENTS like '%$keyword%')";
		if($date)
		{
			list($year, $month, $day) = explode("-", $date);
			$start = mktime(0,0,0,$month,$day,$year);
			$end = mktime(23,59,59,$month,$day,$year);
			$sql .= " AND (p.F_POSTS_ISSUE_DATE >= $start AND p.F_POSTS_ISSUE_DATE <= $end)";
		}
		switch($status)
		{
			case 1:
				$sql .= " AND p.F_POSTS_STATUS = 1";
				break;
			case 2:
				$sql .= " AND (p.F_POSTS_STATUS = 1 OR p.F_POSTS_STATUS = 2)";
				break;
			default:
				break;
		}
		$r = $this->select($sql);
		return $r[0][0];
	}
	/**
	 * 功能：提取二级分类列表
	 * 参数：$blogid 博客ID
	 * 返回：数组
	 */
	public function GetCatList($blogid, $name, $cat1FID=0)
	{
		$sql = "SELECT * FROM $name c WHERE F_ID_BLOG_INFO = $blogid";
		if($cat1FID > 0)
			$sql .= " AND c.F_ID_CATEGORIES1 = $cat1FID";
//		$sql .= " ORDER BY F_ID DESC";
		return $this->select($sql);
	}
	/**
	 * 功能：删除分类，把该分类下的文章转移到默认分类
	 * 参数：$catid 分类ID,$blogid 博客ID
	 * 返回：TRUE OR FALSE
	 */
	public function DelClass($catid, $blogid, $name)
	{
		$this->begintransaction();
		try{
			$sql = "DELETE FROM $name WHERE F_ID = $catid AND F_ID_BLOG_INFO = $blogid";
			$this->delete($sql);
		}catch(Exception $e){									//捕获异常，回滚
			$this->rollback();
			return false;
		}
		$this->commit();
		return true;
	}
	/**
	 * 功能：删除文章
	 * 参数：$userid 用户ID,$postid 文章ID
	 * 返回：TRUE OR FALSE;
	 */
	public function DelPost($userid, $postid)
	{
		$r = $this->getInfo($postid, "ee_blog_posts");
		$this->begintransaction();

		try{
			$sql = "UPDATE ee_blog_categories2 SET F_CATEGORIES_POSTS = F_CATEGORIES_POSTS-1 WHERE F_ID = {$r['F_ID_CATEGORIES']}";
			$this->update($sql);

			$sql = "DELETE FROM ee_blog_posts WHERE F_ID = $postid AND F_ID_USER_INFO = $userid";
			$this->delete($sql);
		}catch (exception $e){
			$this->rollback();
			return false;
		}

		$this->commit();
		return true;
	}
	/**
	 * 功能：更新分类文章数
	 * 参数：$catid 分类ID,$type 更新类型("+"或"-")
	 * 返回：TRUE OR FALSE
	 */
	public function UpdateCatPosts($catid,$type='+')
	{
		$sql = "UPDATE ee_blog_categories2 SET F_CATEGORIES_POSTS = F_CATEGORIES_POSTS $type 1";
		$sql .= " WHERE F_ID = $catid";
		return $this->update($sql);
	}
	/**
	* 提起文章详细信息
	* 参数：$id文章id
	*/
	public function GetPosts($id){
		$sql = "SELECT c.F_CATEGORIES_NAME,p.F_POSTS_TITLE,p.F_POSTS_CONTENTS,p.F_POSTS_STATUS";
		$sql .= ",p.F_ID,c.F_ID as CatId,p.F_POSTS_IS_COMMENTED,p.F_POSTS_COMMENTS,p.F_POSTS_ISSUE_DATE,p.F_POSTS_VIEWS";
		$sql .= " FROM ee_blog_categories2 c, ee_blog_posts p WHERE p.F_ID = '$id' AND p.F_ID_CATEGORIES = c.F_ID";
		$r = $this->select($sql);
		return $r[0];
	}
	/**
	* 更新文章访问次数
	* 参数：$postid文章id
	*/
	public function UpdatePostView($postid){
		$sql = "UPDATE ee_blog_posts SET F_POSTS_VIEWS = F_POSTS_VIEWS + 1 WHERE F_ID = $postid";
		return $this->update($sql);
	}
	/**
	* 提取blog相关信息
	* 参数：$blogid博客id
	* 返回：数组
	*/
	public function GetBlogInfo($blogid){
		$sql = "SELECT u.*,b.* FROM ee_blog_user u,em_blog_info b WHERE u.F_ID_BLOG_INFO = b.F_ID AND b.F_ID = $blogid";
		$r = $this->select($sql);
		return $r[0];
	}
	/**
	 * 功能：提取评论列表
	 * 参数：$blogid 博客ID,$postid 文章ID,$pagesize 提取的条数,$page当前页码
	 * 返回：数组
	 */
	public function GetCommentsList($blogid, $postid=0, $pagesize=0, $page=1){
		//if(!$pagesize) $pagesize = $this->pagesize;

		$sql = "SELECT * FROM ee_blog_comments WHERE F_ID_BLOG_INFO = $blogid";
		if($postid)
			$sql .= " AND F_ID_POSTS_INFO = $postid";
		$sql .= " ORDER BY F_COMMENTS_DATE DESC";
		if($pagesize>0){
			$start = ($page - 1)*$pagesize;
			$sql .= " LIMIT $start,$pagesize";
		}
		//$sql .= " ORDER BY F_COMMENTS_DATE DESC";
		return $this->select($sql);
	}
	/**
	 * 功能：提取评论条数
	 * 参数：$postid 文章ID
	 * 返回：条数
	 */
	public function GetCommentsCount($postid=0){
		$sql = "SELECT COUNT(F_ID) FROM ee_blog_comments";
		if($postid) $sql .= " WHERE F_ID_POSTS_INFO = $postid";
		$r = $this->select($sql);
		return $r[0][0];
	}
	/**
	 * 功能：删除评论
	 * 参数：ComId
	 * 返回：true or false
	 */
	public function DelComments($ComId){
		$r = $this->getInfo($ComId, 'ee_blog_comments');
		$this->begintransaction();
		try{
			$sql = "UPDATE ee_blog_posts SET F_POSTS_COMMENTS = F_POSTS_COMMENTS - 1 WHERE F_ID = {$r['F_ID_POSTS_INFO']}";
			$this->update($sql);
			$sql = "DELETE FROM ee_blog_comments WHERE F_ID = $ComId";
			$this->delete($sql);
		}catch(Exception $e){
			$this->rollback();
			return false;
		}
		$this->commit();
		return true;
	}
	/**
	 * 功能：更新文章的评论数
	 * 参数：$postid 文章ID
	 */
	public function UpdatePostsComments($postid){
		$sql = "UPDATE ee_blog_posts SET F_POSTS_COMMENTS = F_POSTS_COMMENTS +1";
		$sql .= " WHERE F_ID = $postid";
		return $this->update($sql);
	}
	/**
	* 功能：提取模板信息
	* 返回：数组
	*/
	public function GetSkinsList(){
		$sql = "SELECT * FROM em_blog_skins";
		return $this->select($sql);
	}
	/**
	 * 功能：设置BLOG信息
	 * 参数：$blogid 博客ID，$array 表单数据
	 * 返回：TRUE OR FALSE
	 */
	public function BlogSet($blogid, $formMethod)
	{
		$this->begintransaction();
		try{
			$data = array();
			$data['F_BLOG_NAME'] = $formMethod['blogname'];
			$data['F_BLOG_DESCRIPTION'] = $formMethod['description'];
			$data['F_BLOG_KEYWORDS'] = $formMethod['keyword'];
			$data['F_BLOG_DEFAULT_SKINS'] = $formMethod['skins'];
			$this->updateData('em_blog_info', $blogid, $data);
			if($formMethod['lock']=='on')
			{
				$lock = 1;
			}else{
				$lock = 0;
			}
			if($formMethod['comStatus']=='on')
			{
				$comStatus = 1;
			}else{
				$comStatus = 0;
			}
			$sql = "UPDATE ee_blog_user SET F_BLOG_IS_LOCKED = $lock,";
			$password = md5($formMethod['password']);
			$sql .= "F_BLOG_PASSWORD = '$password',F_BLOG_PERM_COMMENTS = $comStatus WHERE F_ID_BLOG_INFO = $blogid";
			$this->update($sql);
		}catch(Exception $e){
			$this->rollback();
			return false;
		}
		$this->commit();
		return true;
	}
}
?>